var Config = require('@/utils/config.js');
let BASEURL = Config.baseUrl
import { initVueI18n } from '@dcloudio/uni-i18n'  
import messages from '@/locale/index' 
import Vue from 'vue'
const vm = new Vue()
const { t } = initVueI18n(messages) 
export const request = (options = {}) => {
	return new Promise((resolve, rejects) => {
		handleRequest(options, resolve, rejects)
	})
}
// 发起请求
function handleRequest(options, resolve, reject) {
	const token = uni.getStorageSync('token')||""
	const language =JSON.parse(uni.getStorageSync('languageInfo'))
	uni.showLoading();
	uni.request({
		url: BASEURL+options.url,
		method: options.method,
		header: {
			'Authorization':token?'Bearer '+token:'',
			'content-language':language.type
		},
		timeout:20000,
		data: options.data,
		sslVerify:false,
		success: (response) => {
			if(response.data.code==401){
				uni.$u.toast(t('err.login'))
			    return setTimeout(()=>{
					uni.reLaunch({
						url:'/pages/login/login'
					})
				},1200)
			}
			return resolve(response)
		},
		fail: (err) => {
			uni.$u.toast(t('err.network'))
			return reject(err);
		},
		complete() {
			uni.hideLoading();
		}
	})
}